<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="./css/style.css">
    <!--引入Vue-->
    <script src="./js/axios.min.js"></script>
    <script src="./js/main.js"></script>
    <script src="../js/vue.js"></script>
    <title>充值</title>
    <style>
        #main {
            width: 94%;
            margin: 0 auto;
        }

        .list>li>a {
            margin: 10px;
            background-color: white;
            border-radius: 20px;
            padding: 10px;
            float: left;
            min-width: 200px;
            width: calc(90vw / 4 - 40px);
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="nav flex-container-between">
            <div>
                <ul class=" flex-container-start">
                    <li><a href="./home.html">主页</a></li>
                    <li><a href="./recharge.html">充值</a></li>
                    <li><a href="./increase.html">添加</a></li>
                </ul>
            </div>
            <div>
                <ul class=" flex-container-end">
                    <li><a @click="exit()">退出登录</a></li>
                </ul>
            </div>
        </div>
        <div class="card">
            <h1>电量充值</h1>
            <h3>用户：{{uesr_data.username}}</h3>
            <h3>剩余电量：{{uesr_data.remain}}</h3>
        </div>
        <div id="main">
            <ul class="list">
                <li v-for="(item, index) in position" :key="index">
                    <a @click.prevent="recharge(item)" href="">
                        <h1>￥{{item}}</h1>
                        <h3 style="text-align: right;">{{(item/0.65).toFixed(1)}}KW</h3>
                    </a>
                </li>
            </ul>
        </div>
    </div>

    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                position: [1, 2, 5, 10, 20, 50, 100, 200, 500, 1000],
                uesr_data: {
                    username: '',
                    remain: 0,
                    power: 0,
                },
            },
            methods: {
                recharge(num) {
                    axios.post('/user/recharge', {
                            money: num,
                        })
                        .then((response) => {
                            const responsedata = response.data
                            console.log(response)
                            // 服务器状态码
                            if (responsedata.code != 0) {
                                // 如果不为0，说明有错误, msg为云端错误信息
                                alert(responsedata.msg);
                                // 截断后面的运行
                                if (responsedata.code == 401)
                                    window.location.href = "./login.html";
                                return;
                            }
                            alert("已成功充值" + num + "元")
                            axios.get('/user/info')
                                .then((response) => {
                                    const responsedata = response.data
                                    console.log(response)
                                    // 服务器状态码
                                    if (responsedata.code != 0) {
                                        // 如果不为0，说明有错误, msg为云端错误信息
                                        alert(responsedata.msg);
                                        // 截断后面的运行
                                        if (responsedata.code == 401)
                                            window.location.href = "./login.html";
                                        return;
                                    }
                                    // 下面的代码只有在服务器返回状态码为0时才会执行, 也就是说服务器返回正确的数据
                                    this.uesr_data = responsedata.data;
                                })
                                .catch((error) => {
                                    console.log(error);
                                });
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                },
                exit() {
                    axios.post('/user/logout')
                        .then((response) => {
                            const responsedata = response.data
                            // 服务器状态码
                            if (responsedata.code != 0) {
                                // 如果不为0，说明有错误, msg为云端错误信息
                                alert(responsedata.msg);
                                // 截断后面的运行
                                return;
                            }
                            window.location.href = "./login.html";
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            },
            beforeCreate() { //生命周期初始化前
                axios.get('/user/info')
                    .then((response) => {
                        const responsedata = response.data
                        console.log(response)
                        // 服务器状态码
                        if (responsedata.code != 0) {
                            // 如果不为0，说明有错误, msg为云端错误信息
                            alert(responsedata.msg);
                            // 截断后面的运行
                            if (responsedata.code == 401)
                                window.location.href = "./login.html";
                            return;
                        }
                        // 下面的代码只有在服务器返回状态码为0时才会执行, 也就是说服务器返回正确的数据
                        this.uesr_data = responsedata.data;

                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
        })
    </script>
</body>

</html>